/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/12 16:06
 * @Copyright
 */
package multithreading;

public class ThreadTest16 {
    public static void main(String[] args) throws InterruptedException {
        MyClass2 mc1=new MyClass2();
        MyClass2 mc2=new MyClass2();

        Processor16 p1=new Processor16(mc1);
        Processor16 p2=new Processor16(mc2);

        Thread t1=new Thread(p1);
        t1.setName("t1");
        Thread t2=new Thread(p2);
        t2.setName("t2");

        //启动线程
        t1.start();

        //延迟(保证t1先启动，并执行run)
        Thread.sleep(1000);
        t2.start();
    }
}
class Processor16 implements Runnable{
    MyClass2 mc;
    public  Processor16(MyClass2 mc){
        this.mc=mc;
    }
    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("t1")){
            mc.m1();
        }
        if (Thread.currentThread().getName().equals("t2")){
            mc.m2();
        }
    }
}
class MyClass2{
    public synchronized void m1(){
        //休眠
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("m1.......");
    }
    //
    public synchronized void m2(){
        System.out.println("m2......");
    }
}